﻿package {
	
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.display.MovieClip;

	
	public class Motion extends Sprite{
		
		//private var pirate:Sprite;
		//private var merchant:Sprite;
		private var collision:Sprite;
		private var collision1:Sprite;
		private var time:Number = 0;
		private var ldr:Loader ;
		private var url:String;
		private var urlReq:URLRequest;
		private var pir:pirateShip;
		private var mer:merchantShip;
		private var pir1:pirateShip;
		private var mer1:merchantShip;
		private var pir2:pirateShip;
		
		
		
		public function Motion(){
			
			init();
		}
		

		private function init():void{
			
			//pirate = new Sprite();
			//merchant = new Sprite();
			collision = new Sprite();
			collision1 = new Sprite();
			ldr = new Loader();
			url = "Map.jpg";
			urlReq = new URLRequest(url);
			ldr.load(urlReq);
			pir = new pirateShip();
			mer = new merchantShip();
			pir1 = new pirateShip();
			mer1 = new merchantShip();
			pir2 = new pirateShip();
			
			addChild(ldr);
			//addChild(pirate);
			//addChild(merchant);
			addChild(collision);
			addChild(collision1);
			addChild(pir);
			addChild(mer);
			addChild(pir1);
			addChild(mer1);
			addChild(pir2);
			
			
			//pirate.graphics.beginFill(0xff0000);
			//pirate.graphics.drawCircle(0,0,10);
			//pirate.graphics.endFill();
			
			pir.x = 0;
			pir.y = 0;
			pir1.x = 400;
			pir1.y = 300;
			pir2.x = 250;
			pir2.y = 400;
			
			//merchant.graphics.beginFill(0xff00ff);
			//merchant.graphics.drawCircle(0,0,30);
			//merchant.graphics.endFill();
			//merchant.graphics.lineStyle(3,0xff0000);
			mer.x = 0;
			mer.y = 400;
			mer1.x = 300;
			mer1.y = 0;
			
			
			time = 0;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		
		
		public function onEnterFrame(event:Event):void{
			
			if(pir.y <= 280 && pir.x <= 200){
				pir.y += (0.007)*time; 
				pir.x += (0.005)*time;
			}
			if(pir1.y >= 133 && pir1.x >= 167){
				pir1.y += (-0.005)*time; 
				pir1.x += (-0.007)*time;
		}
				pir2.y += (-0.007)*time; 
				pir2.x += (0.005)*time;
			//trace(pir.x);
			//trace(pir.y);
			//pir.graphics.lineStyle(2,0x00ff00);
			//pir.graphics.lineTo(200,280);
			//pir.graphics.moveTo(pir.x,pir.y);
			
			//pirate.graphics.moveTo(pirate.x,pirate.y);
		
			if(time % 2 == 0 && pir.y <= 280)
				collision.graphics.beginFill(0x0f00f0);
			else 
				collision.graphics.beginFill(0xff0f0);
			collision.graphics.drawCircle(200,280,10);
			collision.graphics.endFill();
			
			if(time % 2 == 0 && pir1.y >= 133)
				collision1.graphics.beginFill(0x0ff0f0);
			else 
				collision1.graphics.beginFill(0xf0ff0);
			collision1.graphics.drawCircle(167,133,10);
			collision1.graphics.endFill();
			
			if(mer.y >= 280 && mer.x <= 200){
				mer.y += (-0.003)*time; 
				mer.x += (0.004)*time;
			}
			if(mer1.y <= 133 && mer1.x >= 167){
				mer1.y += (0.004)*time; 
				mer1.x += (-0.003)*time;
			}
			//merchant.graphics.moveTo(merchant.x,merchant.y);
			//merchant.graphics.lineTo(merchant.x + (0.004)*5,merchant.y + (-0.003)*5);
			//merchant.graphics.moveTo(merchant.x,merchant.y);
			//trace(mer.x);
			//trace(mer.y);
		
			if(pir.y < stage.stageHeight)
				time++;
			else
				time = 0;
				
			//trace(time);
			
			
		}

	}
}